package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.utils.BrightnessUtils;

/**
 * Created by ray_lei on 2016/8/19.
 * 添加措施
 */
public class NursingFunctionActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private WebView webView;
    private ProgressBar pb_web;

    private String url;
    private String title;
    private String questionId = "";
    private String questionUserId = "";
    private String userUuid = "";
    private String patientId = "";
    private String remindId = "";

    //两个按钮
    private PopupWindow popup_easynotice;
    private TextView tv_easynotice_title;
    private TextView tv_easynotice_cancel;
    private TextView tv_easynotice_func;

    private boolean isBack = false;

    @Override
    protected void updateByHandler(Message msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_articledetail);

        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");
//        questionId = getIntent().getStringExtra("questionId");
        if (getIntent().hasExtra("questionUserId")) {
            questionUserId = getIntent().getStringExtra("questionUserId");
        }
        patientId = getIntent().getStringExtra("patientId");
        isBack = getIntent().getBooleanExtra("back", false);

        initView();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(title);

        webView = (WebView) findViewById(R.id.webview_article);
        pb_web = (ProgressBar) findViewById(R.id.pb_article);

        WebSettings webSettings = webView.getSettings();

        webSettings.setJavaScriptEnabled(true); //支持js
        webSettings.setUseWideViewPort(true);  //将图片调整到适合webview的大小
        webSettings.setSupportZoom(true);  //支持缩放
        webSettings.setDomStorageEnabled(true);

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                pb_web.setProgress(newProgress);
                if (newProgress == 100) {
                    pb_web.setVisibility(View.INVISIBLE);
                }
                super.onProgressChanged(view, newProgress);
            }

        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                System.out.println("url====" + url);
                if (url.contains("patientInfo")) {
                    remindId = url.split("=")[1];
                    tv_easynotice_title.setText(getResources().getString(R.string.generate_remind));
                    tv_easynotice_cancel.setText(getResources().getString(R.string.generate_remind_yes));
                    tv_easynotice_func.setText(getResources().getString(R.string.generate_remind_no));
                    popup_easynotice.showAtLocation(ll_title, Gravity.CENTER, 0, 0);
                    BrightnessUtils.lightOff(NursingFunctionActivity.this);

                    return true;
                } else if (url.contains("backInfo")) {
                    finish();
                }
                return super.shouldOverrideUrlLoading(view, url);
            }

        });
        webView.loadUrl(url + "&questionId=" + questionId + "&questionUserId=" + questionUserId + "&userId=" + loginInfo.getUserId() + "&acctType=" + loginInfo.getUserStyle());

        //easynotice
        popup_easynotice = new PopupWindow(this);
        View viewNotice = LayoutInflater.from(this).inflate(R.layout.popup_easynotice, null);
        popup_easynotice.setContentView(viewNotice);

        popup_easynotice.setWidth((int) (screenWidth * 0.7));
        popup_easynotice.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup_easynotice.setFocusable(true);
        popup_easynotice.setTouchable(true);
        popup_easynotice.setOutsideTouchable(true);
        popup_easynotice.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popup_easynotice.setAnimationStyle(R.style.popupwindow_anim_alpha);

        tv_easynotice_title = (TextView) viewNotice.findViewById(R.id.tv_easynotice_title);
        tv_easynotice_cancel = (TextView) viewNotice.findViewById(R.id.tv_easynotice_cancel);
        tv_easynotice_func = (TextView) viewNotice.findViewById(R.id.tv_easynotice_func);

        tv_easynotice_cancel.setOnClickListener(this);
        tv_easynotice_func.setOnClickListener(this);

        popup_easynotice.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(NursingFunctionActivity.this);
            }
        });

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                webView.loadUrl("javascript:back()");
//                finish();
                break;
            case R.id.tv_easynotice_cancel:
                popup_easynotice.dismiss();
//                Intent intent1 = new Intent(NursingFunctionActivity.this, DolistPreviewActivity.class);
//                intent1.putExtra("id", remindId);
//                intent1.putExtra("type", 1);
//                intent1.putExtra("patientId", patientId);
//                intent1.putExtra("back", isBack);
//                startActivityForResult(intent1, 100);
                Intent intent1 = new Intent(NursingFunctionActivity.this, BetterPlanEditActivity.class);
                intent1.putExtra("orderId", remindId);
                intent1.putExtra("isAutoCreate", true);
                intent1.putExtra("patientId", patientId);
                intent1.putExtra("back", isBack);
                startActivityForResult(intent1, 100);
                break;
            case R.id.tv_easynotice_func:
                popup_easynotice.dismiss();
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 100) {
                if (data.getBooleanExtra("back", false)) {
                    Intent iBack = new Intent();
                    iBack.putExtra("back", isBack);
                    setResult(RESULT_OK, iBack);
                    finish();
                }
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            webView.loadUrl("javascript:back()");
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
